<?php

require_once("share_api.php");
require_once("forum_api.php");
require_once("user_api.php");
require_once("utilites_inc.php");

class gui_api
{
	private $db;
	
	public function __construct($db) 
	{
		$this->db = $db;
    }
	
	
	private function insert_share_node_image($id, $name, $desc, $filename, $user_id, $user_name, $category_name, $likeit_count)
	{
		?>
<div class="box col2">        
	<div class="box-content">                        
		<div class="img-container">    
			<a href="<?=SERVER_URL?>project/<?=$id?>/<?=translate_url_looklike($name)?>"><img alt="<?=$name?>" src="<?=SERVER_URL . GALLERY_THUMB_PATH . $filename?>" /></a>
        </div>
                                                                     
        <div class="post-content">
			<h2><a title="<?=$name?>" href="<?=SERVER_URL?>project/<?=$id?>/<?=translate_url_looklike($name)?>"><?=$name?></a></h2>	            	            	    
            <p><?=strlen($desc) > 250 ? mb_substr($desc, 0, 250, "UTF-8") . "..." : $desc?></p>            
		</div>
          
        <div class="post-footer">
        	<a href="<?=SERVER_URL . "profile/" . $user_id . "/" . translate_url_looklike($user_name)?>" class="link"><?=$user_name?></a> | <a href="#" class="link"><?=$category_name?></a>
			
            <?php if(is_active_user_session()) { ?>			
            <a href="#" class="likeit" onclick="_gui_control.on_click_likeit(this, <?=$id?>); return false;"><?=empty($likeit_count) ? 0 : $likeit_count?></a>
			<?php } else { ?>
			<span class="likeit"><?=empty($likeit_count) ? 0 : $likeit_count?></span>
			<?php } ?>
        </div>                        
      </div>
</div>
        <?php
	}
	
	private function insert_share_node_video($id, $name, $desc, $video, $video_type, $user_id, $user_name, $category_name, $likeit_count)
	{
		?>
<div class="box col2">        
	<div class="box-content">                        
		<div class="img-container">
        	<?php if($video_type == VIDEO_YOUTUBE_TYPE) { ?>               
            <a href="<?=SERVER_URL?>project/<?=$id?>/<?=translate_url_looklike($name)?>"><img src="http://img.youtube.com/vi/<?=$video?>/0.jpg" alt="<?=$name?>" /></a>                       
            <?php } else if($video_type == VIDEO_VIMEO_TYPE) {
				$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$video.php"));
			?>            
    		<a href="<?=SERVER_URL?>project/<?=$id?>/<?=translate_url_looklike($name)?>"><img src="<?=$hash[0]['thumbnail_medium']?>" alt="<?=$name?>" /></a>
			<?php } ?> 
        </div>                                                                    
        <div class="post-content">
			<h2><a title="<?=$name?>" href="<?=SERVER_URL?>project/<?=$id?>/<?=translate_url_looklike($name)?>"><?=$name?></a></h2>	            	            	    
            <p><?=strlen($desc) > 250 ? mb_substr($desc, 0, 250, "UTF-8") . "..." : $desc?></p>
		</div>
          
        <div class="post-footer">
        	<a href="<?=SERVER_URL . "profile/" . $user_id . "/" . translate_url_looklike($user_name)?>" class="link"><?=$user_name?></a> | <a href="#" class="link"><?=$category_name?></a>
			
			<?php if(is_active_user_session()) { ?>			
            <a href="#" class="likeit" onclick="_gui_control.on_click_likeit(this, <?=$id?>); return false;"><?=empty($likeit_count) ? 0 : $likeit_count?></a>
			<?php } else { ?>
			<span class="likeit"><?=empty($likeit_count) ? 0 : $likeit_count?></span>
			<?php } ?>
        </div>                        
      </div>
</div>
        <?php
	}
	
	public function insert_comment_node($text, $user_id, $user_name, $user_image, $date)
	{
		?>
        <div class="comment-box">        	
        	<a href="<?=SERVER_URL?>/profile/<?=$user_id?>" class="profile-image"><img src="<?=SERVER_URL?>gfx/user-empty.png" alt="<?=$user_name?>" /></a>
            <div class="content">
	            <a href="<?=SERVER_URL?>/profile/<?=$user_id?>" class="profile-name"><?=$user_name?></a>
    	        <div class="date"><?=translateDateTime($date, DATE_TRANSLATE_SIMPLE_WORD)?></div>

                <div class="default-text text"><?=nl2br($text)?></div>              
            </div>
            <div class="clear"></div>
        </div>
        <?php
	}
	
	public function insert_share_list($array_param)
	{
		$share_api = new share_api($this->db);
		
		switch($array_param["type"])
		{
		case SHARE_TYPE_GLOBAL:
			$result = $share_api->get_share_list();
			break;	
			
		case SHARE_TYPE_USER:
			$result = $share_api->get_share_user_list($array_param["user_id"]);
			break;
		}	
		
		ob_start();
		
		while($row = $result->fetch_assoc()) 
		{
			switch($row["PRJ_TYPE"])
			{
				case PROJECT_TYPE_IMAGE:
					$this->insert_share_node_image($row["PRJ_ID"], $row["PRJ_NAME"], $row["PRJ_DESC"], $row["PRI_FILENAME"], $row["USR_ID"], $row["USR_NAME"], 
						$row["CAT_NAME"], $row["LIKEIT_COUNT"]);
					break;
					
				case PROJECT_TYPE_VIDEO:
				$this->insert_share_node_video($row["PRJ_ID"], $row["PRJ_NAME"], $row["PRJ_DESC"], $row["PRV_VIDEO"], $row["PRV_TYPE"], $row["USR_ID"], $row["USR_NAME"],
					$row["CAT_NAME"], $row["LIKEIT_COUNT"]);
					break;
					
				case PROJECT_TYPE_TEXT:
					break;
			}			
		}
				
		$result->free();
				
		return ob_get_clean();
	}
	
	
	public function insert_forum_list($project_id, $page)
	{
		$forum_api = new forum_api($this->db);
		
		if($result = $forum_api->get_comment_list($project_id, $page))
		{					
			ob_start();				
								
			while($row = $result->fetch_assoc()) 
			{								
				$this->insert_comment_node($row["CMT_TEXT"], $row["USR_ID"], $row["USR_NAME"], $row["USR_IMAGE"], $row["CMT_DATE"]);											
			}
			
			$result->free();
					
			return ob_get_clean();
		}
	}
	
	public function insert_forum_pagination($project_id, $current_page)
	{
		$forum_api = new forum_api($this->db);
		
		$comments_count = $forum_api->get_comments_count($project_id);
		
		$max_page = ceil($comments_count / MAX_VIEW_FORUM_COMMENTS);
		
		
		if($current_page < $max_page - 1)
		{
			ob_start();
			
			?><button onclick="_gui_control.on_click_comments_paginator(<?=$project_id?>, <?=$current_page + 1?>);">show more comments</button><?php
			
			return ob_get_clean();	
		}				
	}
	
	private function insert_feed_text()
	{
		
	}
	
	private function insert_feed_video($id, $video, $video_type, $name, $desc)
	{
		?>
		<div class="feed-item">
			<?php if($video_type == VIDEO_YOUTUBE_TYPE) { ?>               
            <a href="<?=SERVER_URL?>project/<?=$id?>/<?=translate_url_looklike($name)?>"><img src="http://img.youtube.com/vi/<?=$video?>/default.jpg" alt="<?=$name?>" /></a>                       
            <?php } else if($video_type == VIDEO_VIMEO_TYPE) {
				$hash = unserialize(file_get_contents("http://vimeo.com/api/v2/video/$video.php"));
			?>            
    		<a href="<?=SERVER_URL?>project/<?=$id?>/<?=translate_url_looklike($name)?>"><img src="<?=$hash[0]['thumbnail_small']?>" alt="<?=$name?>" /></a>
			<?php } ?> 
			<div class="content">
				<a href="<?=SERVER_URL?>project/<?=$id?>/<?=translate_url_looklike($name)?>" class="title"><?=$name?></a>
				<div class="text"><?=strlen($desc) > 250 ? mb_substr($desc, 0, 100, "UTF-8") . "..." : $desc?></div>
			</div>
			<div class="clear"></div>
		</div>
		<?php
	}
	
	private function insert_feed_image($id, $filename, $name, $desc)
	{
		?>	
		<div class="feed-item">
			<a href="<?=SERVER_URL?>project/<?=$id?>/<?=translate_url_looklike($name)?>"><img alt="<?=$name?>" src="<?=SERVER_URL . GALLERY_THUMB_PATH . $filename?>" /></a>
			<div class="content">
				<a href="<?=SERVER_URL?>project/<?=$id?>/<?=translate_url_looklike($name)?>" class="title"><?=$name?></a>
				<div class="text"><?=strlen($desc) > 250 ? mb_substr($desc, 0, 100, "UTF-8") . "..." : $desc?></div>
			</div>
			<div class="clear"></div>
		</div>
		<?php
	}
	
	public function insert_feed_list($type, $limit)
	{	
		$share_api = new share_api($this->db);
		
		switch($type)
		{
		case FEED_TYPE_RECOMENDED:
			$result = $share_api->get_share_feed_list($limit);
			break;
			
		case FEED_TYPE_SUBSCRIPTION:
			$result = $share_api->get_subscribers_feed_list($_SESSION["user_id"], $limit);
			break;
			
		default:
			$result = null;
			break;
		}
		
		
		if($result)
		{			
			ob_start();
			
			?><div class="feed-list"><?php
			
			while($row = $result->fetch_assoc()) 
			{
				switch($row["PRJ_TYPE"])
				{
				case PROJECT_TYPE_IMAGE:
					$this->insert_feed_image($row["PRJ_ID"], $row["PRI_FILENAME"], $row["PRJ_NAME"], $row["PRJ_DESC"]);
					break;
					
				case PROJECT_TYPE_VIDEO:
					$this->insert_feed_video($row["PRJ_ID"], $row["PRV_VIDEO"], $row["PRV_TYPE"], $row["PRJ_NAME"], $row["PRJ_DESC"]);
					break;
					
				case PROJECT_TYPE_TEXT:
					break;
				}
				
			}
			
			?></div><?php
			
			$result->free();
			
			return ob_get_clean();
		}
	}
}

?>